import api from './index'

export interface Homework {
  id: number
  title: string
  description?: string
  problems: number[]
  deadline?: string
  teacher_id: number
  created_at: string
  problem_details?: Problem[]
}

export interface Problem {
  id: number
  title: string
  description: string
  type: string
}

export interface CreateHomeworkRequest {
  title: string
  description?: string
  problems: number[]
  deadline?: string
}

export const homeworksApi = {
  // 获取所有作业
  getHomeworks(): Promise<Homework[]> {
    return api.get('/homeworks')
  },

  // 获取指定作业
  getHomework(id: number): Promise<Homework> {
    return api.get(`/homeworks/${id}`)
  },

  // 创建作业
  createHomework(data: CreateHomeworkRequest): Promise<Homework> {
    return api.post('/homeworks', data)
  },

  // 更新作业
  updateHomework(id: number, data: Partial<CreateHomeworkRequest>): Promise<Homework> {
    return api.put(`/homeworks/${id}`, data)
  },

  // 删除作业
  deleteHomework(id: number): Promise<void> {
    return api.delete(`/homeworks/${id}`)
  },

  // 获取指定教师的作业
  getHomeworksByTeacher(teacherId: number): Promise<Homework[]> {
    return api.get(`/homeworks/teacher/${teacherId}`)
  },

  // 获取我的作业（教师）
  getMyHomeworks(): Promise<Homework[]> {
    return api.get('/homeworks/my-homeworks')
  },

  // 搜索作业
  searchHomeworks(params: { keyword?: string; teacher_id?: number }): Promise<Homework[]> {
    return api.get('/homeworks/search', { params })
  }
}
